package com.tbsoft.trail.util;

import java.io.StringWriter;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Transformer;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
/**
 * @desc 把bean序列化成xml的工具类
 * @author lizhenhua
 *
 */
public class BeanToXmlParse {
	private static VelocityEngine veClassPath;
	private Map<String, Object> argMap = new HashMap<String, Object>();

	static {
		veClassPath = new VelocityEngine();
		veClassPath.setProperty("resource.loader", "class");
		veClassPath.setProperty("class.resource.loader.class",
		"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
		veClassPath.setProperty(Velocity.ENCODING_DEFAULT, "GBK");
		veClassPath.setProperty(Velocity.INPUT_ENCODING, "GBK");
		veClassPath.setProperty(Velocity.OUTPUT_ENCODING, "GBK");
		try {
			veClassPath.init();
		} catch (Exception ex) {
			throw new RuntimeException("初始化Velocity异常", ex);
		}
	}

	public static BeanToXmlParse getInstance() {
		BeanToXmlParse beanToXmlPares = new BeanToXmlParse();
		return beanToXmlPares;
	}

	private BeanToXmlParse() {

	}
	/**
	 * @desc 根据模板序列化对象
	 * @param templatePath 模板位置
	 * @return 序列化后的xml
	 * @throws Exception
	 */
	public String parse(String templatePath) throws Exception {
		String result = "";
		if (templatePath.toLowerCase().contains("jar!")) {
			VelocityEngine ve = new VelocityEngine();
			ve.setProperty("resource.loader", "jar");
			ve.setProperty("jar.resource.loader.class",
				"org.apache.velocity.runtime.resource.loader.JarResourceLoader");

			int end = templatePath.indexOf("jar") + 3;
			int start = templatePath.indexOf("file");

			String jarPath = "jar:" + templatePath.substring(start, end);
			ve.setProperty("jar.resource.loader.path", jarPath);
			try {
				ve.init();
			} catch (Exception ex) {
				throw new Exception("初始化Velocity异常", ex);
			}

			start = templatePath.toLowerCase().indexOf("jar!") + 5;
			String resource = templatePath.substring(start);

			result = doParse(resource, ve);
		} else {
			result = doParse(templatePath, veClassPath);
		}

		return result;

	}
	/**
	 * @desc 序列化
	 * @param templatePath
	 * @param ve
	 * @return
	 * @throws Exception
	 */
	private String doParse(String templatePath, VelocityEngine ve)
			throws Exception {
		VelocityContext context = new VelocityContext();

		Set<Entry<String, Object>> argSet = argMap.entrySet();
		StringWriter s = new StringWriter();

		for (Entry<String, Object> entry : argSet)
			context.put(entry.getKey(), entry.getValue());

		try {

			Template t = ve.getTemplate(templatePath);
			t.setEncoding("GBK");
			t.merge(context, s);

		} catch (Exception ex) {
			throw new Exception("使用vm生成xml出现异常", ex);
		}
		return s.toString();
	}
	/**
	 * @desc 放入对象
	 * @param name
	 * @param object
	 * @param transformer
	 */
	@SuppressWarnings("unchecked")
	public void putArgs(String name, Object object, Transformer transformer) {
		if (transformer != null) {

			if (object instanceof Collection) {
				CollectionUtils.transform((Collection) object, transformer);
			}
		}

		this.argMap.put(name, object);
	}

}
